package cn.edu.xmu.oomall.aftersales.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.IdNameTypeDto;
import cn.edu.xmu.javaee.core.model.dto.PageDto;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import cn.edu.xmu.javaee.core.validation.NewGroup;
import cn.edu.xmu.oomall.aftersales.controller.vo.AfterSalesVo;
import cn.edu.xmu.oomall.aftersales.dao.bo.AfterSales;
import cn.edu.xmu.oomall.aftersales.service.AfterSalesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.edu.xmu.javaee.core.model.Constants.PLATFORM;

/**
 * 售后申请控制器
 */
@RestController
@RequestMapping(value = "/shops/{did}", produces = "application/json;charset=UTF-8")
@Transactional
public class AfterSalesController {

    private final Logger logger = LoggerFactory.getLogger(AfterSalesController.class);
    private final AfterSalesService afterSalesService;

    @Autowired
    public AfterSalesController(AfterSalesService afterSalesService) {
        this.afterSalesService = afterSalesService;
    }

    /**
     * 提交售后申请
     *
     * @param id orderItem ID
     * @param user 当前登录用户
     * @param vo 售后申请参数
     * @return 售后申请结果
     */
    @PostMapping("/orderitems/{id}/aftersales")
    public ReturnObject applyForAfterSales(@PathVariable Long id, @LoginUser UserDto user,
                                           @Validated(NewGroup.class) @RequestBody AfterSalesDto dto) {
        //数值对象
        AfterSales afterSales = CloneFactory.copy(new AfterSales(), dto);
        AfterSales newAfterSales = this.afterSalesService.applyForAfterSales(id, afterSales, user);
        IdNameTypeDto vo = IdNameTypeVo.builder().id(newAfterSales.getId()).name(newAfterSales.getName()).build();
        return new ReturnObject(ReturnNo.CREATED, dto);
    }

}
